/**
 * This version adapts some content from "Tracker", a sample robot.
 * 
 * Copyright (c) 2001, 2010 Mathew A. Nelson and Robocode contributors
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://robocode.sourceforge.net/license/epl-v10.html
 *
 * Contributors:
 *     Mathew A. Nelson
 *     - Initial implementation
 *     Flemming N. Larsen
 *     - Maintainance
 */

package jdk;

import java.awt.Color;
import robocode.Robot;
import robocode.ScannedRobotEvent;

/**
 * Robot targets and tracks target once aquired.
 * 
 * @author David Korse (derivative)
 * @version 1.0
 */
public class Rasta01 extends Robot {
  
  int count = 0;
  private int direction = 1;
  double gunTurnAmt = 10;
  String target = null;
  

  /**
   * This main run function sweeps gun to scan.
   * 
   * Adapted from:
   * Tracker - a sample robot by Mathew Nelson, and maintained by Flemming N. Larsen
   */
  public void run() {
    
    setBodyColor(Color.RED);
    setGunColor(Color.GREEN);
    setRadarColor(Color.black);
    setAdjustGunForRobotTurn(true); // Keep the gun still when we turn
    
    while (true) {     
      turnGunRight(gunTurnAmt); 

      // If we've haven't seen our target for 2 turns, look left
      if (count > 2) {
        gunTurnAmt = -60;
      }
      // If we still haven't seen our target for 5 turns, look right
      if (count > 5) {
        gunTurnAmt = 30;
      }
      // If we *still* haven't seen our target after 11 turns, find another target
      if (count > 11) {
        target = null;
        ahead(100);
        count++;
      }
    }
  }

  /**
   * Robot sets first scanned enemy as target, fires, moves "erraticaly" to avoid.
   *
   * @param e scanned robot.
   */
  public void onScannedRobot(ScannedRobotEvent e) {
    
    fire(3);
    if (target == null) {
      target = e.getName();
    }
    else if (target != null && !e.getName().equals(target)) {
      return;
    }    
    count = 0;
    for (int i = 0; i <= 2; i++) {
      if (getVelocity() == 0) {
        direction *= -1;  
        ahead(75 * direction);
      }
    turnRight(15);
    ahead(150);
    }
    gunTurnAmt = -30;
    scan();
  }
  
  /**
   * Gets the current value of 'count'.
   * @return count is the current value of the counter for empty scans.
   */
  public int getTrackCount() {
    return count;
  }
  
  
}
